package ru.edu.niimm.mapping.linguistic;

import LemmatizerWrapper.Wordform;
import LemmatizerWrapper.Paradigm;

import java.util.List;

import gate.util.Out;

/**
 * Created by IntelliJ IDEA.
 * User: Pooh
 * Date: 19.07.2007
 * Time: 18:37:25
 * To change this template use File | Settings | File Templates.
 */
public class Template {
    int wordCount;
    List<GramUnit> units;

    public Template()
    {
    }

    public Template(int wordCount, List<GramUnit> units) {
        this.wordCount = wordCount;
        this.units = units;
    }

    public GramUnit getMainUnit()
    {
        for(GramUnit unit : units.toArray(new GramUnit[0]))
        {
            if(unit.isMainUnit)
            {
                return unit;
            }
        }
        return null;
    }

    private boolean checkTemplateOn1Word(List<Wordform> candidate)
    {
            Wordform wf = candidate.get(0);
            if(wf.paradigms != null)
            {
                for (Paradigm para : wf.paradigms)
                {
                    if(this.getMainUnit().getPos().equalsIgnoreCase(para.pos))
                    {
                        if(wf.gram_codes != null)
                        {
                            for (String[] gram_code : wf.gram_codes)
                            {
                                for (String aGram_code : gram_code)
                                {
                                    if(aGram_code.contains(this.getMainUnit().gramCodeValue)
                                            && !aGram_code.contains("0,"))
                                    {
                                        Out.println(wf.word);
                                        return true;
                                    }
                                }
                            }
                        }
                    }

                }
            }
        
        return false;
    }


    private boolean checkTemplateOn2Words(List<Wordform> candidate)
    {
        for(int k = 0; k < candidate.size(); k++)
            {
                if(candidate.get(k).paradigms != null)
                {
//                    for (Paradigm para : candidate.get(k).paradigms)
//                    {
                    Paradigm para = candidate.get(k).paradigms[0];
                        for(GramUnit unit1 : units.toArray(new GramUnit[0]))
                        {
                            if(unit1.getPos().equalsIgnoreCase(para.pos))
                            {   // POS matches

                                if(candidate.get(1 - k).paradigms != null)
                                {
//                                    for (Paradigm para2 : candidate.get(1 - k).paradigms)
//                                    {
                                    Paradigm para2 = candidate.get(1 - k).paradigms[0];
                                        for(GramUnit unit2 : units.toArray(new GramUnit[0]))
                                        {
                                            if(unit2 != unit1 && unit2.getPos().equalsIgnoreCase(para2.pos))
                                            {
                                                //POS matches !!!
                                                Wordform wf1 = candidate.get(k);
                                                Wordform wf2 = candidate.get(1 - k);
                                                if(wf1.gram_codes != null)
                                                {
                                                    for (String[] gram_code : wf1.gram_codes)
                                                    {
                                                        for (String aGram_code : gram_code)
                                                        {
                                                            if(wf2.gram_codes != null && aGram_code.contains(unit1.gramCodeValue)
                                                                    && !aGram_code.contains("0,"))
                                                            {
                                                                for (String[] gram_code2 : wf2.gram_codes)
                                                                {
                                                                    for (String aGram_code2 : gram_code2)
                                                                    {
                                                                        if(aGram_code2.contains(unit2.gramCodeValue) &&
                                                                                !aGram_code2.contains("0,"))
                                                                        {
                                                                            Out.println(wf1.word + " " + /*"@" + wf1.paradigms[0].pos + aGram_code + "\n " +*/ wf2.word  /* + "@"+ wf2.paradigms[0].pos + aGram_code2 + "--------\n"*/);
                                                                            return true;
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
//                                }
//
//                            }
                        }
                    }
                }
                // у к-го нет ни одной парадигмы
                else
                {

                }
            }
            return false;
    }

    private boolean checkTemplateOn3Words(List<Wordform> candidate)
    {
        if(candidate.get(0).paradigms != null)
            {
                for (Paradigm para0 : candidate.get(0).paradigms)
                {
                   GramUnit unit0 = this.getMainUnit();

                        if(unit0.getPos().equalsIgnoreCase(para0.pos))
                        {
                            for(int k = 2; k >= 1; k--)
                            {
                                if(candidate.get(k).paradigms != null)
                                {
                                    for (Paradigm para : candidate.get(k).paradigms)
                                    {
                                        for(GramUnit unit : units.toArray(new GramUnit[0]))
                                        {
                                            if(unit.getPos().equalsIgnoreCase(para.pos))
                                            {
                                                if(candidate.get(3 - k).paradigms != null)
                                                {
                                                    for (Paradigm para2 : candidate.get(3 - k).paradigms)
                                                    {
                                                        for(GramUnit unit2 : units.toArray(new GramUnit[0]))
                                                        {
                                                            if(unit2 != unit && unit2.getPos().equalsIgnoreCase(para2.pos))
                                                            {
                                                                //POS matches !!!
                                                                Wordform wf1 = candidate.get(k);
                                                                Wordform wf2 = candidate.get(3 - k);
                                                                if(wf1.gram_codes != null)
                                                                {
                                                                    for (String[] gram_code : wf1.gram_codes)
                                                                    {
                                                                        for (String aGram_code : gram_code)
                                                                        {
                                                                            if(wf2.gram_codes != null && aGram_code.contains(unit.gramCodeValue)
                                                                                    && !aGram_code.contains("0,"))
                                                                            {
                                                                                for (String[] gram_code2 : wf2.gram_codes)
                                                                                {
                                                                                    for (String aGram_code2 : gram_code2)
                                                                                    {
                                                                                        if(aGram_code2.contains(unit2.gramCodeValue) &&
                                                                                                !aGram_code2.contains("0,"))
                                                                                        {
                                                                                            Out.println(candidate.get(0).word + " " + candidate.get(1).word + " " + candidate.get(2).word);
                                                                                            return true;
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }

                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return false;
    }

    public boolean checkTemplateOn(List<Wordform> candidate)
    {
        if(wordCount != candidate.size())
        {
            return false;
        }

        if(wordCount == 1)
        {
            return  checkTemplateOn1Word(candidate);
        }

        if(wordCount == 2 )
        {
           return  checkTemplateOn2Words(candidate);
        }

        if(wordCount == 3 )
        {
            return  checkTemplateOn3Words(candidate);
        }
        return false;
    }

    public boolean checkTemplateOn(String date)
    {
        return date.matches("[1-2][0-9][0-9][0-9]");
    }
    
    private boolean isCandidateCoherence(List<Wordform> candidate)
    {
        Wordform wf1 = candidate.get(0);
        Wordform wf2 = candidate.get(1);
        if(wf1.gram_codes != null)
        {
            for (String[] gram_code : wf1.gram_codes)
            {
                for (String aGram_code : gram_code)
                {
                    if(wf2.gram_codes != null)
                    {
                        for (String[] gram_code2 : wf2.gram_codes)
                        {
                            for (String aGram_code2 : gram_code2)
                            {
                                if(isGramCodesCoherence(aGram_code, aGram_code2))
                                {
                                    return true;
                                }
                            }
                        }
                    }
                }
            }
        }
        return false;
    }
    private boolean isGramCodesCoherence(String code1, String code2)
    {
        String[] codes1 = code1.split(",");
        String[] codes2 = code2.split(",");

        return false;
    }
}
